import { defineStore } from 'pinia';

export const useUserStore = defineStore('user', {
  state: () => ({
    userInfo: {
      avatar: '/static/images/default-avatar.png',
      nickname: '用户',
      gender: '保密',
      bio: '这个人很懒，什么都没有留下',
      location: '未知'
    },
    settings: {
      theme: 'light', // light或dark
      notificationsEnabled: true,
      privacyMode: false,
      autoSave: true
    }
  }),
  
  getters: {
    getUserInfo: (state) => {
      return state.userInfo;
    },
    
    getSettings: (state) => {
      return state.settings;
    }
  },
  
  actions: {
    updateUserInfo(userInfo) {
      this.userInfo = {
        ...this.userInfo,
        ...userInfo
      };
      this.saveUserInfoToStorage();
    },
    
    updateSettings(settings) {
      this.settings = {
        ...this.settings,
        ...settings
      };
      this.saveSettingsToStorage();
    },
    
    toggleTheme() {
      this.settings.theme = this.settings.theme === 'light' ? 'dark' : 'light';
      this.saveSettingsToStorage();
    },
    
    toggleNotifications() {
      this.settings.notificationsEnabled = !this.settings.notificationsEnabled;
      this.saveSettingsToStorage();
    },
    
    loadUserInfoFromStorage() {
      try {
        const storedUserInfo = uni.getStorageSync('userInfo');
        if (storedUserInfo) {
          this.userInfo = JSON.parse(storedUserInfo);
        }
      } catch (e) {
        console.error('加载用户信息失败:', e);
      }
    },
    
    loadSettingsFromStorage() {
      try {
        const storedSettings = uni.getStorageSync('settings');
        if (storedSettings) {
          this.settings = JSON.parse(storedSettings);
        }
      } catch (e) {
        console.error('加载设置信息失败:', e);
      }
    },
    
    saveUserInfoToStorage() {
      try {
        uni.setStorageSync('userInfo', JSON.stringify(this.userInfo));
      } catch (e) {
        console.error('保存用户信息失败:', e);
      }
    },
    
    saveSettingsToStorage() {
      try {
        uni.setStorageSync('settings', JSON.stringify(this.settings));
      } catch (e) {
        console.error('保存设置信息失败:', e);
      }
    }
  }
}); 